package Wormish.agiletre.se;

import android.content.Context;
import android.os.Handler;
import android.os.Message;
import android.util.AttributeSet;
import android.util.Log;
import android.view.KeyEvent;
import android.view.SurfaceHolder;
import android.view.SurfaceView;

public class GameView extends SurfaceView implements SurfaceHolder.Callback
{
    /** Handle to the application context, used to e.g. fetch Drawables. */
    private Context mContext;
    
    /** The thread that actually draws the animation */
    private GameThread myGameThread;
    
    /** Pointer to the text view to display "Paused.." etc. */
    //private TextView mStatusText;
    
    public GameView(Context context, AttributeSet attrs)
    {
        super(context, attrs);
        
        // register our interest in hearing about changes to our surface
        SurfaceHolder holder = getHolder();
        holder.addCallback(this);

        // create thread only; it's started in surfaceCreated()
        //myGameThread = new GameThread(holder, context, null);
        myGameThread = new GameThread(holder, context, new Handler()
        {
            @Override
            public void handleMessage(Message m)
            {
                //mStatusText.setVisibility(m.getData().getInt("viz"));
                //mStatusText.setText(m.getData().getString("text"));
            }
        });

        setFocusable(true); // make sure we get key events
    }
    
    /**
     * Standard override to get key-press events.
     */
    @Override
    public boolean onKeyDown(int keyCode, KeyEvent msg)
    {
        return myGameThread.doKeyDown(keyCode, msg);
    }
    
    /**
     * Standard override for key-up. We actually care about these, so we can
     * turn off the engine or stop rotating.
     */
    @Override
    public boolean onKeyUp(int keyCode, KeyEvent msg)
    {
        return myGameThread.doKeyUp(keyCode, msg);
    }
    
    /**
     * Standard window-focus override. Notice focus lost so we can pause on
     * focus lost. e.g. user switches to take a call.
     */
    @Override
    public void onWindowFocusChanged(boolean hasWindowFocus)
    {
        if (!hasWindowFocus)
        {
        	//myGameThread.pause();
        }
    }
    
    /*
     * Callback invoked when the Surface has been created and is ready to be
     * used.
     */
    public void surfaceCreated(SurfaceHolder holder)
    {
        // start the thread here so that we don't busy-wait in run()
        // waiting for the surface to be created
    	myGameThread.setRunning(true);
    	myGameThread.start();
    }
    
    /*
     * Callback invoked when the Surface has been destroyed and must no longer
     * be touched. WARNING: after this method returns, the Surface/Canvas must
     * never be touched again!
     */
    public void surfaceDestroyed(SurfaceHolder holder)
    {
        // we have to tell thread to shut down & wait for it to finish, or else
        // it might touch the Surface after we return and explode
        boolean retry = true;
        myGameThread.setRunning(false);
        
        while (retry)
        {
            try
            {
                myGameThread.join();
                retry = false;
            }
            catch (InterruptedException e)
            {
            }
        }
    }
    
    /* Callback invoked when the surface dimensions change. */
    public void surfaceChanged(SurfaceHolder holder, int format, int width, int height)
    {
        myGameThread.setSurfaceSize(width, height);
    }
    
    /**
     * Fetches the animation thread corresponding to this LunarView.
     * 
     * @return the animation thread
     */
    public GameThread getThread()
    {
        return myGameThread;
    }
}
